﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using QRCoder;

namespace Kele.Activity.Core
{
    public static class AppUtility
    {

        public static string GenerateId(string prefix="")
        {
            long i = 1;
            foreach (var b in Guid.NewGuid().ToByteArray())
            {
                i *= b + 1;
            }
            return $"{prefix}s{i - DateTime.Now.Ticks:x}";
        }

        public static string GetOrderId(int merchant_id)
        {
            //生成订单号
            //订单号生成原则：年（4位）+月（2位）+日（2位）+时（2位）+分（2位）+秒（2位）+商家编号（5位，不够左补0）+5位随机数，2018 10 10 21 30 2 1      00001 43261

            //商家编号（5位，不够左补0）
            string merchant = merchant_id.ToString();
            merchant = merchant.PadLeft(5, '0');     // 共5位，之前用0补齐

            string num = BuildRandomStr(5);//自动生成一个5位随机数

            string ordernum = DateTime.Now.ToString("yyyy") + DateTime.Now.ToString("MM") + DateTime.Now.ToString("dd") +
            DateTime.Now.ToString("hh") + DateTime.Now.ToString("mm") + DateTime.Now.ToString("ss") +
            merchant + num;

            return ordernum;
        }

        public static Random random = new Random();

        /// <summary>
        /// 取随机数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string BuildRandomStr(int length)
        {
            int num;

            lock (random)
            {
                num = random.Next();
            }

            string str = num.ToString();

            if (str.Length > length)
            {
                str = str.Substring(0, length);
            }
            else if (str.Length < length)
            {
                int n = length - str.Length;
                while (n > 0)
                {
                    str = str.Insert(0, "0");
                    n--;
                }
            }

            return str;
        }



            public static string GetBase64QRCode(string plainText)
        {
            var qrGenerator = new QRCodeGenerator();
            var qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q);
            var qrCode = new PngByteQRCode(qrCodeData);
            var pngImage = qrCode.GetGraphic(10);
            return Convert.ToBase64String(pngImage);
        }
        public static List<string> QueryImage(string imageString)
        {
            
            var list = new List<string>();
            if (string.IsNullOrWhiteSpace(imageString)) return list;
            var images = JArray.Parse(imageString).ToList();
            foreach (var image in images)
            {
                list.Add(image["url"].Value<string>());
            }
            return list;
        }
        public static string QueryImageFirst(string imageString)
        {
            if (string.IsNullOrWhiteSpace(imageString)) return string.Empty;
            var images = JArray.Parse(imageString);
            return images.Count == 0 ? string.Empty : images[0]["url"].Value<string>();
        }


          public static string GetDirectoryPath(string path)
        {
            var ext = Path.GetExtension(path);
            var directoryPath = !string.IsNullOrEmpty(ext) ? Path.GetDirectoryName(path) : path;
            return directoryPath;
        }

           public static string GetFileName(string filePath)
        {
            return Path.GetFileName(filePath);
        }
    }
}